package net.lab0.google.ex1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Ex1
{
    public static void main(String[] args)
    {
        Map<Character, Character> mapping = new HashMap<Character, Character>();
        
        // the 1st example
        mapping.put('y', 'a');
        mapping.put('e', 'o');
        mapping.put('q', 'z');
        
        // printMapping(mapping);
        fillTheMapWithTheExample(mapping);
        findTheMissingLetter(mapping);
        printMapping(mapping);
        readAndTranslate(mapping);
    }
    
    private static void readAndTranslate(Map<Character, Character> mapping)
    {
        Scanner in = new Scanner(System.in); // console doesn't work inside eclipse :(
        int totalTests = in.nextInt();
        in.nextLine();
        for (int i = 0; i < totalTests; ++i)
        {
            String s = in.nextLine();
            System.out.println("Case #" + (i + 1) + ": " + translate(s, mapping));
        }
        in.close();
    }
    
    private static String translate(String s, Map<Character, Character> mapping)
    {
        StringBuilder out = new StringBuilder();
        for (int i = 0; i < s.length(); i++)
        {
            out.append(mapping.get(s.charAt(i)));
        }
        return out.toString();
    }
    
    private static void findTheMissingLetter(Map<Character, Character> mapping)
    {
        char a = 'a';
        Set<Character> abc = new HashSet<>();

        for (int i = 0; i < 26; ++i)
        {
            abc.add((char)(a+i));
        }
        
        Character nulledChar = null;
        for (int i = 0; i < 26; ++i)
        {
            Character in = (char) (a + i);
            Character mappedChar = mapping.get(in);
            if (mappedChar == null)
            {
                nulledChar = in;
            }else{
                abc.remove(mappedChar);
            }
        }
        
        Character survivor = abc.iterator().next();
        mapping.put(nulledChar, survivor);
    }
    
    private static void printMapping(Map<Character, Character> mapping)
    {
        List<Character> sortedKeys = new ArrayList<>(mapping.keySet());
        Collections.sort(sortedKeys);
        
        List<Character> sortedValues = new ArrayList<>(mapping.values());
        Collections.sort(sortedValues);
        
        for (Character key : sortedKeys)
        {
            System.out.println(key + " -> " + mapping.get(key) + " : " + (key - mapping.get(key)));
        }
        
        StringBuilder sb = new StringBuilder(26 + 5);
        sb.append("Sorted in =");
        for (Character c : sortedKeys)
        {
            sb.append(c);
        }
        
        StringBuilder sb2 = new StringBuilder(26 + 5);
        sb2.append("Sorted out =");
        for (Character c : sortedKeys)
        {
            sb2.append(c);
        }
        
        System.out.println(sb.toString());
        System.out.println(sb2.toString());
    }
    
    public static void fillTheMapWithTheExample(Map<Character, Character> mapping)
    {
        String input = "ejp mysljylc kd kxveddknmc re jsicpdrysi rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd de kr kd eoya kw aej tysr re ujdr lkgc jv";
        String output = "our language is impossible to understand there are twenty six factorial possibilities so it is okay if you want to just give up";
        
        for (int i = 0; i < input.length(); ++i)
        {
            char c1 = input.charAt(i);
            char c2 = output.charAt(i);
            
            if (mapping.get(c1) != null && !mapping.get(c1).equals(c2))
            {
                System.out.println("There is something fishy here.");
            }
            else
            {
                mapping.put(c1, c2); // overwrite thanks to the check above
            }
        }
    }
}
